using System;
using System.Collections.Generic;
using UnityEngine;

public class CustomisationImportPreview : MonoBehaviour
{
	[Serializable]
	public class PersonaModelConfig
	{
		public PersonaModel model;

		public CustomisationConfig.ShaderUseType shaderUseType;

		public CharacterRotationController controller;

		public Camera camera;

		public Transform transformToRotate;

		public CharacterRotationController.RotationControllerSettings settings;

		public bool isEnabled;
	}

	public UIScrollView view;

	public UITable table;

	public LookSetPicker pickerPrefab;

	public Persona.Gender gender;

	public PersonaModel model;

	private List<LookSetPicker> pickers = new List<LookSetPicker>();

	public List<PersonaModelConfig> modelConfigs = new List<PersonaModelConfig>();

	private Dictionary<int, CustomisationConfig.Look> activeLooks = new Dictionary<int, CustomisationConfig.Look>();

	private int setCount;

	private Dictionary<Persona.Gender, Persona> genderSpecificLooks = new Dictionary<Persona.Gender, Persona>();

	public void ApplySelectedLook(CustomisationConfig.Look look, int setId)
	{
		if (!activeLooks.ContainsKey(setId))
		{
			activeLooks.Add(setId, null);
		}
		activeLooks[setId] = look;
		foreach (PersonaModelConfig modelConfig in modelConfigs)
		{
			look.ApplyLook(modelConfig.model, modelConfig.shaderUseType);
		}
	}

	private void OnEnable()
	{
		CreateLookSetPickers();
		foreach (PersonaModelConfig modelConfig in modelConfigs)
		{
			if (!(modelConfig.controller == null))
			{
				modelConfig.controller.SetRotationEnabled(modelConfig.camera, modelConfig.transformToRotate, modelConfig.settings, modelConfig.isEnabled);
			}
		}
	}

	private void CreateLookSetPickers()
	{
		activeLooks.Clear();
		setCount = CustomisationConfig.instance.lookSets.Count;
		for (int i = 0; i < CustomisationConfig.instance.lookSets.Count; i++)
		{
			if (i >= pickers.Count)
			{
				LookSetPicker component = NGUITools.AddChild(table.gameObject, pickerPrefab.gameObject).GetComponent<LookSetPicker>();
				component.gameObject.name = GGFormat.IndexName(i);
				pickers.Add(component);
			}
			pickers[i].Init(CustomisationConfig.instance.lookSets[i], i, this);
		}
		for (int j = CustomisationConfig.instance.lookSets.Count; j < pickers.Count; j++)
		{
			pickers[j].gameObject.SetActive(false);
		}
		table.Reposition();
	}

	private void Update()
	{
		if (setCount != CustomisationConfig.instance.lookSets.Count)
		{
			CreateLookSetPickers();
			return;
		}
		Persona myPersona = new Persona(gender);
		myPersona = SelectGender(myPersona, gender);
		for (int i = 0; i < CustomisationConfig.instance.lookSets.Count; i++)
		{
			int num = 0;
			foreach (CustomisationConfig.Look look in CustomisationConfig.instance.lookSets[i].looks)
			{
				if (look.IsAvailable(myPersona))
				{
					num++;
				}
			}
			if (pickers[i].looksCount != num)
			{
				CreateLookSetPickers();
				return;
			}
		}
		foreach (KeyValuePair<int, CustomisationConfig.Look> activeLook in activeLooks)
		{
			foreach (PersonaModelConfig modelConfig in modelConfigs)
			{
				activeLook.Value.ApplyLook(modelConfig.model, modelConfig.shaderUseType);
			}
		}
		if (Input.GetKeyDown(KeyCode.P))
		{
			if (Time.timeScale == 0f)
			{
				Time.timeScale = 1f;
			}
			else
			{
				Time.timeScale = 0f;
			}
		}
	}

	private void SaveGenderSpecificLooks(Persona myPersona)
	{
		genderSpecificLooks[myPersona.gender] = myPersona;
	}

	private Persona SelectGender(Persona myPersona, Persona.Gender newGender)
	{
		Persona persona = new Persona(newGender);
		Persona persona2 = null;
		if (genderSpecificLooks.ContainsKey(newGender))
		{
			persona2 = genderSpecificLooks[newGender];
		}
		foreach (Persona.LookSelection item in myPersona.myConfig)
		{
			if (item.selectedLook != null && item.selectedLook.IsAvailable(persona))
			{
				continue;
			}
			if (item.selectedLook != null)
			{
				CustomisationConfig.Look similarGenderLook = GetSimilarGenderLook(item.selectedLook.lookName, newGender, item.type);
				if (similarGenderLook != null)
				{
					persona.ChangeLook(similarGenderLook);
					continue;
				}
			}
			if (persona2 == null)
			{
				continue;
			}
			foreach (Persona.LookSelection item2 in persona2.myConfig)
			{
				if (item.type == item2.type && item2.selectedLook != null && item2.selectedLook.IsAvailable(persona))
				{
					persona.ChangeLook(item2.selectedLook);
					break;
				}
			}
		}
		myPersona = persona;
		return myPersona;
	}

	private CustomisationConfig.Look GetSimilarGenderLook(string name, Persona.Gender gender, CustomisationConfig.LookSetType type)
	{
		if (type == CustomisationConfig.LookSetType.Hair)
		{
			return null;
		}
		CustomisationConfig.LookSet lookSetForType = CustomisationConfig.instance.GetLookSetForType(type);
		return lookSetForType.GetLookByName(name, gender);
	}
}
